<?php
/*-------------------------------------------------------+
| PHP-Fusion Content Management System
| Copyright (C) 2002 - 2008 Nick Jones
| http://www.php-fusion.co.uk/
+--------------------------------------------------------+
| Filename: jquery_shoutbox_functions.php
| Author: bartek124
| E-Mail: bartek124@php-fusion.pl
| Web: http://bartek124.php-fusion.pl
+--------------------------------------------------------+
| This program is released as free software under the
| Affero GPL license. You can redistribute it and/or
| modify it under the terms of this license which you
| can read by viewing the included agpl.txt or online
| at www.gnu.org/licenses/agpl.html. Removal of this
| copyright header is strictly prohibited without
| written permission from the original author(s).
+--------------------------------------------------------*/
function sbwrap($text) {
	
	$i = 0; $tags = 0; $chars = 0; $res = "";
	
	$str_len = strlen($text);
	
	for ($i = 0; $i < $str_len; $i++) {
		$chr = substr($text, $i, 1);
		if ($chr == "<") {
			if (substr($text, ($i + 1), 6) == "a href" || substr($text, ($i + 1), 3) == "img") {
				$chr = " ".$chr;
				$chars = 0;
			}
			$tags++;
		} elseif ($chr == "&") {
			if (substr($text, ($i + 1), 5) == "quot;") {
				$chars = $chars - 5;
			} elseif (substr($text, ($i + 1), 4) == "amp;" || substr($text, ($i + 1), 4) == "#39;" || substr($text, ($i + 1), 4) == "#92;") {
				$chars = $chars - 4;
			} elseif (substr($text, ($i + 1), 3) == "lt;" || substr($text, ($i + 1), 3) == "gt;") {
				$chars = $chars - 3;
			}
		} elseif ($chr == ">") {
			$tags--;
		} elseif ($chr == " ") {
			$chars = 0;
		} elseif (!$tags) {
			$chars++;
		}
		
		if (!$tags && $chars == 18) {
			$chr .= "<br />";
			$chars = 0;
		}
		$res .= $chr;
	}
	
	return $res;
}

function ajax_parsesmileys($message) {
	global $smiley_cache;
		if (!$smiley_cache) { cache_smileys(); }
		if (is_array($smiley_cache) && count($smiley_cache)) {
			foreach ($smiley_cache as $smiley) {
				$smiley_code = preg_quote($smiley['smiley_code']);
				$smiley_image = "<img src='".$smiley['smiley_image']."' alt='".$smiley['smiley_image']."' style='vertical-align:middle;' class='smiley' />";
				$message = preg_replace("#{$smiley_code}#si", $smiley_image, $message);
			}
		}
	return $message;
}

function utf2iso($string) {
	$utf_iso = array(
		"\xc4\x85" => "\xb1",
		"\xc4\x84" => "\xa1",
		"\xc4\x87" => "\xe6",
		"\xc4\x86" => "\xc6",
		"\xc4\x99" => "\xea",
		"\xc4\x98" => "\xca",
		"\xc5\x82" => "\xb3",
		"\xc5\x81" => "\xa3",
		"\xc3\xb3" => "\xf3",
		"\xc3\x93" => "\xd3",
		"\xc5\x9b" => "\xb6",
		"\xc5\x9a" => "\xa6",
		"\xc5\xba" => "\xbc",
		"\xc5\xb9" => "\xac",
		"\xc5\xbc" => "\xbf",
		"\xc5\xbb" => "\xaf",
		"\xc5\x84" => "\xf1",
		"\xc5\x83" => "\xd1",
		// xmlhttprequest uses different encoding
		"%u0104" => "\xA1",
		"%u0106" => "\xC6",
		"%u0118" => "\xCA",
		"%u0141" => "\xA3",
		"%u0143" => "\xD1",
		"%u00D3" => "\xD3",
		"%u015A" => "\xA6",
		"%u0179" => "\xAC",
		"%u017B" => "\xAF",
		"%u0105" => "\xB1",
		"%u0107" => "\xE6",
		"%u0119" => "\xEA",
		"%u0142" => "\xB3",
		"%u0144" => "\xF1",
		"%u00D4" => "\xF3",
		"%u015B" => "\xB6",
		"%u017A" => "\xBC",
		"%u017C" => "\xBF"
	);
	return str_replace(array_keys($utf_iso), array_values($utf_iso), $string);
}
?>